//
//  BrandViewController.m
//  CoolTennisBall
//
//  Created by wsl on 16/1/4.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "BrandViewController.h"

@interface BrandViewController ()<UIPickerViewDataSource,UIPickerViewDelegate,UIGestureRecognizerDelegate>

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UIView *tapView;

@property (copy, nonatomic) NSArray *dataSource;
@property (weak, nonatomic) IBOutlet UILabel *titleDescriptionLabel;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
@property (weak, nonatomic) IBOutlet UIButton *okButton;

@end

@implementation BrandViewController




#pragma mark - View LifeCircle / Setter/Getter

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.pickerView selectRow:self.dataSource.count / 2 inComponent:0 animated:YES];
    
    [self configureLocalization];
    [self addGestureRecognizer];

}

- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource = @[NSLocalizedString(@"OLIVER", nil),
                        NSLocalizedString(@"BABOLAT", nil),
                        NSLocalizedString(@"BONNY", nil),
                        NSLocalizedString(@"KAWASAKI", nil),
                        NSLocalizedString(@"DUNLOP", nil),
                        NSLocalizedString(@"DECATHLON", nil),
                        NSLocalizedString(@"FLEX", nil),
                        NSLocalizedString(@"FISCHER", nil),
                        NSLocalizedString(@"HEAD", nil),
                        NSLocalizedString(@"HANWEN", nil),
                        NSLocalizedString(@"DHS", nil),
                        NSLocalizedString(@"PROKENNEX", nil),
                        NSLocalizedString(@"MESUCA", nil),
                        NSLocalizedString(@"QIANGJING", nil),
                        NSLocalizedString(@"QIANGLI", nil),
                        NSLocalizedString(@"SLAZENGER", nil),
                        NSLocalizedString(@"STAR", nil),
                        NSLocalizedString(@"SILLK", nil),
                        NSLocalizedString(@"TECNIFIBRE", nil),
                        NSLocalizedString(@"TELOON", nil),
                        NSLocalizedString(@"PRINCE", nil),
                        NSLocalizedString(@"WISH", nil),
                        NSLocalizedString(@"VOLKL", nil),
                        NSLocalizedString(@"YONEX", nil),
                        NSLocalizedString(@"WILSON", nil),
                        NSLocalizedString(@"JOEREX", nil),
                        NSLocalizedString(@"OTHERS", nil)];
        
    }
    return _dataSource;
}

#pragma mark - Localization

- (void)configureLocalization
{
    self.titleDescriptionLabel.text = NSLocalizedString(@"Please choose racket brand", nil);
    [self.okButton setTitle:NSLocalizedString(@"Ok", nil) forState:UIControlStateNormal];
    [self.cancelButton setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
}

# pragma mark - IBAction

- (IBAction)cancel {
    [self close];
}

- (IBAction)confirm {
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectedBrand:)]) {
        
        NSInteger selectedRow = [self.pickerView selectedRowInComponent:0];
        NSString * brand = self.dataSource[selectedRow];
        
        [self.delegate selectedBrand:brand];
    }
    [self dismissFromParentViewControllerWithAnimationType:DetailViewControllerAnimationTypeFade];
}


#pragma mark - PickerViewDataSouce

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataSource.count;
}

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    NSString *rowStr = self.dataSource[row];
    NSAttributedString *attribuedString = [[NSAttributedString alloc] initWithString:rowStr attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:25.0f], NSForegroundColorAttributeName: [UIColor colorWithHex:0x7c7c7e]}];
    return attribuedString;
}


#pragma mark - Private Method

- (void)addGestureRecognizer
{
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)];
    gestureRecognizer.cancelsTouchesInView = NO;
    gestureRecognizer.delegate = self;
    [self.tapView addGestureRecognizer:gestureRecognizer];
}

- (void)close
{
    [self dismissFromParentViewControllerWithAnimationType:DetailViewControllerAnimationTypeSlide];
}


#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return self.tapView == touch.view;
}



@end
